package web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import dao.NewsDAO;
import dao.TagDAO;
import dao.UserDAO;

import beans.News;
import beans.Tag;
import beans.User;

public class AdminNewsController extends MultiActionController{
	
	private NewsDAO newsDAO;
	
	private UserDAO userDAO;
	
	private TagDAO tagDAO;
	
	public TagDAO getTagDAO() {
		return tagDAO;
	}

	public void setTagDAO(TagDAO tagDAO) {
		this.tagDAO = tagDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	private LoggedUser loggedUser;

	public void setLoggedUser(LoggedUser loggedUser) {
		this.loggedUser = loggedUser;
	}
	
	public void setNewsDAO(NewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}
	
	public NewsDAO getNewsDAO() {
		return newsDAO;
	}


	@RequestMapping(value = "/adminnews.htm")
	public ModelAndView adminnews(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();

		// System.out.println(news.getTags().size());
		modelMap.addAttribute("user", userDAO.getLoggedUser());
		modelMap.addAttribute("newsList", newsDAO.DisplayNews());
		modelMap.addAttribute("tagList", tagDAO.GetAllTag());
		return new ModelAndView("adminnews", modelMap);
	}
	
	@RequestMapping(value="/adminnewsedit.htm")
    public ModelAndView adminnewsedit(HttpServletRequest request,
            HttpServletResponse response) throws Exception{
           int id = Integer.parseInt(ServletRequestUtils.getRequiredStringParameter(request, "news_id"));
           
           ModelMap modelMap = new ModelMap();
           /*User user = userDAO.getLoggedUser();*/
           News news = newsDAO.findNewsById(id);
           modelMap.addAttribute("news", news);
   		   modelMap.addAttribute("tagList", tagDAO.GetAllTag());
   		   /*modelMap.addAttribute("user", user);*/
           return new ModelAndView("adminnewsedit", modelMap);
   }
	
	@RequestMapping(value="/adminnewsdelete.htm")
	   public ModelAndView adminnewsdelete(HttpServletRequest request,
            HttpServletResponse response) throws Exception{
	          int id = Integer.parseInt(ServletRequestUtils.getRequiredStringParameter(request, "news_id"));
	          
	          News news = newsDAO.findNewsById(id);
	          newsDAO.deleteNews(news);
	  		   
	  		   return new ModelAndView("redirect:adminnews.htm");
	          
		}
	
	@RequestMapping(value="/adminupdatenews.htm")
	   public ModelAndView adminupdatenews(HttpServletRequest request,
	           HttpServletResponse response) throws Exception{
	          int id = Integer.parseInt(ServletRequestUtils.getRequiredStringParameter(request, "news_id"));
	          
	          String title = request.getParameter("title");
	  		  String newss = request.getParameter("news");
	  		  News newnews = newsDAO.findNewsById(id);
	  		  newnews.setTitle(title);
	  		  newnews.setNews(newss);
	  		  User user = newnews.getUser();
	  		  newnews.setUser(user);
	  		  
	  		String newstags[] = request.getParameterValues("tags");
			List<Tag> listTag = new ArrayList<Tag>();
			for(String str : newstags){
				Tag tag = tagDAO.FindTagById(Integer.parseInt(str));
				listTag.add(tag);
			}		
			newnews.setTags(listTag);
	  		  newsDAO.UpdateNews(newnews);
	  		   
	  		   return new ModelAndView("redirect:adminnews.htm");
	          
		}
	
		

}
